﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;

namespace UniMana.Data
{
    public static class Training_Score_TaskInputData
    {
        #region [Hòa]
        /// <summary>
        /// Danh sách phân công theo lớp học phần
        /// </summary>
        /// <param name="nTraining_Score_TaskInput"></param>
        /// <param name="nPersonnel_Personnel"></param>
        /// <param name="_iTraining_Subject_ClassKey">Mã lớp học phần</param>
        /// <param name="_bInput">True là nhập điển, False là kiểm tra điểm</param>
        /// <returns></returns>
        public static IQueryable GetByTrainingClassKey(this Table<Training_Score_TaskInput> nTraining_Score_TaskInput, Table<Personnel_Personnel> nPersonnel_Personnel, int _iTraining_Subject_ClassKey, bool _bInput = true)
        {
            var iQuery = from t in nTraining_Score_TaskInput
                         join t0 in nPersonnel_Personnel on t.Personnel_PersonnelKey equals t0.Personnel_PersonnelKey
                         where
                           t.Training_Subject_ClassKey == _iTraining_Subject_ClassKey
                           && t.Input == _bInput
                           && (t.Lock == null || t.Lock == false)
                         select new
                         {
                             t.Personnel_PersonnelKey,
                             t0.ID,
                             t0.LastName,
                             t0.Name
                             //t.Date,
                             //t.Period,
                             //t.Note,
                             //t.Training_Subject_ClassKey,
                             //t.Training_Score_TaskInputKey
                         };
            return iQuery;
        }

        /// <summary>
        /// Danh sách phân công theo danh sách lớp học phần
        /// </summary>
        /// <param name="nTraining_Score_TaskInput"></param>
        /// <param name="_iTraining_Subject_ClassKey"></param>
        /// <param name="_bInput">True là nhập điển, False là kiểm tra điểm</param>
        /// <returns></returns>
        public static IQueryable GetListByClassKey(this Table<Training_Score_TaskInput> nTraining_Score_TaskInput, int _iTraining_Subject_ClassKey, DateTime _dtenow, bool _bInput = true)
        {
            //var iQuery = nTraining_Score_TaskInput.Where(p => p.Training_Subject_ClassKey == _iTraining_Subject_ClassKey && p.Input == _bInput && (p.Lock == null || p.Lock == false)) ;
            var iQuery = from scoreTaskInput in nTraining_Score_TaskInput
                         where scoreTaskInput.Training_Subject_ClassKey == _iTraining_Subject_ClassKey
                         where scoreTaskInput.Input == _bInput
                         where (scoreTaskInput.Lock == null || scoreTaskInput.Lock == false)
                         select new
                         {
                             scoreTaskInput.Training_Score_TaskInputKey,
                             scoreTaskInput.Training_Subject_ClassKey,
                             scoreTaskInput.Personnel_PersonnelKey,
                             scoreTaskInput.Personnel_PersonnelKeyLeader,
                             scoreTaskInput.Date,
                             scoreTaskInput.Period,
                             scoreTaskInput.Note,
                             scoreTaskInput.Lock,
                             scoreTaskInput.Input,
                             Status = (scoreTaskInput.Date.Value.AddDays(scoreTaskInput.Period.Value) > _dtenow ? (scoreTaskInput.Lock == true ? "Hoàn thành" : "Chưa hoàn thành") : (scoreTaskInput.Lock == true ? "Hoàn thành" : "Không hoàn thành"))
                         };
            return iQuery;
        }

        /// <summary>
        /// Lấy danh sách lớp được phân công theo nhân viên
        /// </summary>
        /// <param name="nTraining_Score_TaskInput"></param>
        /// <param name="nPersonnel_Personnel">Mã phân viên</param>
        /// <param name="_listClassKey">Danh sách mã lớp</param>
        /// <param name="_dteNow">Thời gian hiện tại</param>
        /// <param name="_bInput">True là nhập điển, False là kiểm tra điểm</param>
        /// <returns></returns>
        public static IQueryable GetByTrainingClassKey(this Table<Training_Score_TaskInput> nTraining_Score_TaskInput, Table<Personnel_Personnel> nPersonnel_Personnel, List<int> _listClassKey, DateTime _dteNow, bool _bInput = true)
        {
            var iQuery = from t in nTraining_Score_TaskInput
                         join t0 in nPersonnel_Personnel on t.Personnel_PersonnelKey equals t0.Personnel_PersonnelKey
                         where
                            t.Input == _bInput
                            && t.Date.Value.AddDays(t.Period.Value) > _dteNow
                            && (t.Lock == null || t.Lock == false)
                         select new
                         {
                             t.Personnel_PersonnelKey,
                             t0.ID,
                             t0.LastName,
                             t0.Name,
                             t.Training_Subject_ClassKey
                             //t.Date,
                             //t.Period,
                             //t.Note,
                             //t.Training_Subject_ClassKey,
                             //t.Training_Score_TaskInputKey
                         };
            for (int i = 0; i < _listClassKey.Count; i++)
            {
                iQuery = iQuery.Where(p => p.Training_Subject_ClassKey == _listClassKey[i]);
                if (_listClassKey.Count - 1 == i)
                    break;
            }

            return iQuery;
        }

        /// <summary>
        /// Lấy danh sách phân công theo lớp
        /// </summary>
        /// <param name="nTraining_Score_TaskInput"></param>
        /// <param name="_listClassKeys">Danh sách mã lớp</param>
        /// <param name="_dteNow">Thời gian hiện tại</param>
        /// <param name="_bInput">True là nhập điển, False là kiểm tra điểm</param>
        /// <returns></returns>
        public static IQueryable GetListByClassKey(this Table<Training_Score_TaskInput> nTraining_Score_TaskInput, List<int> _listClassKeys, DateTime _dteNow, bool _bInput = true)
        {
            var iQuery = from t0 in nTraining_Score_TaskInput
                         where
                             t0.Input == _bInput
                             && t0.Date.Value.AddDays(t0.Period.Value) > _dteNow
                             && (t0.Lock == null || t0.Lock == false)
                         select new
                         {
                             t0.Training_Subject_ClassKey,
                             t0.Personnel_PersonnelKey,
                             t0.Date,
                             t0.Period,
                             t0.Note,
                             t0.Input
                         };
            for (int i = 0; i < _listClassKeys.Count; i++)
            {
                iQuery = iQuery.Where(p => p.Training_Subject_ClassKey == _listClassKeys[i]);
                if (_listClassKeys.Count - 1 == i)
                    break;
            }
            return iQuery;
        }

        /// <summary>
        /// Lấy danh sách công việc được giao
        /// </summary>
        /// <param name="nTraining_Score_TaskInput"></param>
        /// <param name="nTraining_Subject_Class"></param>
        /// <param name="nPersonnel_Personnel"></param>
        /// <param name="_gPersonnel_PersonnelKey">Mã nhân viên</param>
        /// <param name="_bInput">True là nhập điển, False là kiểm tra điểm</param>
        /// <returns></returns>
        public static IQueryable GetTask(this Table<Training_Score_TaskInput> nTraining_Score_TaskInput, Table<Training_Subject_Class> nTraining_Subject_Class, Table<Personnel_Personnel> nPersonnel_Personnel, Guid _gPersonnel_PersonnelKey, bool _bInput = true)
        {
            var iQuery = from scoreTaskInput in nTraining_Score_TaskInput
                         from subjectClass in nTraining_Subject_Class
                         from personel in nPersonnel_Personnel
                         where scoreTaskInput.Training_Subject_ClassKey == subjectClass.Training_Subject_ClassKey
                         where scoreTaskInput.Personnel_PersonnelKey == _gPersonnel_PersonnelKey
                         where scoreTaskInput.Input == _bInput
                         where scoreTaskInput.Lock == false
                         where personel.Personnel_PersonnelKey == scoreTaskInput.Personnel_PersonnelKey
                         select new
                         {
                             subjectClass.Training_Subject_ClassKey,
                             subjectClass.ID,
                             subjectClass.Name,
                             scoreTaskInput.Date,
                             scoreTaskInput.Period,
                             FullName = personel.LastName + personel.Name
                         };
            return iQuery;
        }

        /// <summary>
        /// Lấy danh sách lớp theo nhân viên
        /// </summary>
        /// <param name="nTraining_Score_TaskInput"></param>
        /// <param name="nTraining_Subject_Class"></param>
        /// <param name="nPersonnel_Personnel"></param>
        /// <param name="_gPersonnel_PersonnelKey">Mã nhân viên</param>
        /// <param name="now">thời gian hiện tại</param>
        /// <param name="_bInput">True là nhập điển, False là kiểm tra điểm</param>
        /// <returns></returns>
        public static IQueryable GetClassInTask(this Table<Training_Score_TaskInput> nTraining_Score_TaskInput, Table<Training_Subject_Class> nTraining_Subject_Class, Table<Personnel_Personnel> nPersonnel_Personnel, Guid _gPersonnel_PersonnelKey, DateTime now, bool _bInput = true)
        {
            var iQuery = from scoreTaskInput in nTraining_Score_TaskInput
                         from subjectClass in nTraining_Subject_Class
                         from personel in nPersonnel_Personnel
                         where scoreTaskInput.Training_Subject_ClassKey == subjectClass.Training_Subject_ClassKey
                         where scoreTaskInput.Personnel_PersonnelKey == _gPersonnel_PersonnelKey
                         where scoreTaskInput.Input == _bInput
                         where scoreTaskInput.Lock == false
                         where personel.Personnel_PersonnelKey == scoreTaskInput.Personnel_PersonnelKey
                         where scoreTaskInput.Date <= now
                         where scoreTaskInput.Date.Value.AddDays(scoreTaskInput.Period.Value) >= now
                         select new
                         {
                             subjectClass.Training_Subject_ClassKey,
                             subjectClass.ID,
                             subjectClass.Name,
                             scoreTaskInput.Date,
                             scoreTaskInput.Period,
                             FullName = personel.LastName + personel.Name
                         };
            return iQuery;
        }


        public static IQueryable CheckAddInput(this Table<Training_Score_TaskInput> nTraining_Score_TaskInput, Table<Training_Subject_Class> nTraining_Subject_Class, List<int> _ilClassKey, DateTime _dteNow)
        {
            var classInTask = from scoreTaskInput in nTraining_Score_TaskInput
                              from subjectClass in nTraining_Subject_Class
                              where _ilClassKey.Contains(scoreTaskInput.Training_Subject_ClassKey.Value)
                              where scoreTaskInput.Input == false
                              where scoreTaskInput.Lock == false
                              where scoreTaskInput.Date.Value.AddDays(scoreTaskInput.Period.Value) >= _dteNow
                              where subjectClass.Training_Subject_ClassKey == scoreTaskInput.Training_Subject_ClassKey
                              select subjectClass;
            return classInTask;
        }
        #endregion
    }
}
